﻿using System;
using System.Collections.Generic;

using RexToy;
using RexToy.Configuration;

namespace MGAS.Model
{
    static class Configuration
    {
        private const string SENSORS = "sensors";
        private const string SENSOR_ID = "sensor-id";
        private const string READ_LATENCY = "read-latency";
        private const string POLLING_LATENCY = "polling-latency";
        private const string ALLOC_MAP = "alloc-map";

        public const string PORT = "port";
        public static string[] PROPERTIES = { "BaudRate", "DataBits", "Parity", "ReadTimeout", "WriteTimeout" };

        public static int GetReadLatency()
        {
            return AppConfig.Config.ReadValue<int>(SENSORS, READ_LATENCY) ?? 200;
        }

        public static int GetPollingLatency()
        {
            return AppConfig.Config.ReadValue<int>(SENSORS, POLLING_LATENCY) ?? 1500;
        }

        public static string GetAllocMap()
        {
            return AppConfig.Config.ReadValue(SENSORS, ALLOC_MAP);
        }

        public static Dictionary<string, string> GetSerialPortSetting()
        {
            string port = AppConfig.Config.ReadValue(SENSORS, PORT);

            Dictionary<string, string> settings = new Dictionary<string, string>();

            foreach (string key in AppConfig.Config.GetAllKeysInSection(SENSORS))
            {
                if (key.IsPrefixWith(port, PrefixedName.DotDelimiter))
                {
                    PrefixedName pn = new PrefixedName(key, PrefixedName.DotDelimiter);
                    settings[pn.LocalName] = AppConfig.Config.ReadValue(SENSORS, key);
                }
            }

            settings[PORT] = port;
            return settings;
        }
    }
}
